﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using RLZY.others;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Diagnostics;
namespace RLZY
{
    public partial class FormXL_2Moon_DZ : Form
    {
        public FormXL_2Moon_DZ()
        {
            InitializeComponent();
            
        }
        
        private static FormXL_2Moon_DZ childFromInstanc;
        static log4net.ILog LOG = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public String userID;
        public String 权限;


        public static FormXL_2Moon_DZ ChildFromInstanc
        {
            get
            {
                if (childFromInstanc == null || childFromInstanc.IsDisposed)
                //判断childFromInstanc是否有值，不为null或者窗体释放（当你将子窗体关了的时候）为childFromInstanc赋值
                {
                    childFromInstanc = new FormXL_2Moon_DZ();
                }
                return childFromInstanc;
                //返回的childFromInstanc（子窗体存在时返回的是null,关闭了（IsDisposed为true）或者第一次打开子窗体返回一个ChildForm实例）
            }
        }

        private void FormNotKey_GDX_Modify_Load(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;

            dtpDateStart.Text = Convert.ToString(dt.AddMonths(-1)); 
            try
            {
                BLL.userpassword bllpwd = new BLL.userpassword();
                Model.userpassword model = bllpwd.GetModel(Convert.ToInt32(others.UserHelper.UserID));
                others.myGZ mygz = new myGZ();
                权限 = model.备注;
                //this.txtBM.Text = 权限;

                //DataSet ds = mygz.GetListPart(0, 权限);
                //this.BindData(ds);
                
            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        
        private void BindData(DataSet ds)
        {
            DataView dv = new DataView();
            dv.Table = ds.Tables[0];
            //dv.Sort = "ID desc";//反排序
            //bindingSource1.DataSource = dv;
            this.ultraGrid1.DataSource = dv;
        }
        //列选择器
        private void btnCustomColumnChooser_Click(object sender, EventArgs e)
        {
            try
            {
                this.ShowCustomColumnChooserDialog();
            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        private RLZY.CustomColumnChooser customColumnChooserDialog = null;
        
        private void ShowCustomColumnChooserDialog()
        {
            if (null == this.customColumnChooserDialog || this.customColumnChooserDialog.IsDisposed)
            {
                this.customColumnChooserDialog = new RLZY.CustomColumnChooser();
                this.customColumnChooserDialog.Owner = this;
                this.customColumnChooserDialog.Grid = this.ultraGrid1;
            }

            this.customColumnChooserDialog.Show();
        }
        //修改列布局
        private void btnDisplayLayout_Click(object sender, EventArgs e)
        {
            ultraGrid1.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
        }
        //隐藏列
        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            //e.Layout.Bands[0].Columns[0].Hidden = true;
        }

        
        

        
        public void Last_Date()
        {
            others.myGZ mygz = new myGZ();
            DataSet dataSet = mygz.GetListLastDate(权限);
           
            bool flag = dataSet.Tables[0].Rows.Count > 0;
            if (flag)
            {
                this.LastDate = dataSet.Tables[0].Rows[0]["日期"].ToString();
            }
            else
            {
                this.LastDate = "9998-12-31";
            }
            
        }
        
        public string LastDate { get; set; }

        
      
        public string Dat { get; set; }

        
       
        
        //按回车键跳转

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Enter && !(ActiveControl is Button))
            {//碰到Button时不跳转到下一个控件，而是执行Button的单击事件
                keyData = Keys.Tab;
            }
            return base.ProcessDialogKey(keyData);
        }

       
        
        public string VarDat { get; set; }

       
        private void btnDateShow_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.unitJINZHZ(this.cmbBM.Text.Trim(),this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        /// <summary>
        /// Toggles the buttons used for exporting the grid data.
        /// </summary>
        /// <param name="enabled">Boolean, used to signify whether or not to enable/disable the export buttons.</param>
        private void ExportToggle(bool enabled)
        {
            btn2Excel.Enabled = enabled;
            //btnExportPDF.Enabled = enabled;
            //btnExportWord.Enabled = enabled;
            //btnCancelExport.Enabled = !enabled;
        }

        private void btn2Excel_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            string filename = "相邻二月对照_导出" + dt.ToFileTime().ToString();//127756416859912816
            StringBuilder strSql = new StringBuilder();
            strSql.Append("D:\\");
            strSql.Append(filename);
            strSql.Append(".xls");
            string path = strSql.ToString();  //测试一个xls文档
            //ultraGridExcelExporter1.Export(ultraGrid1, path);
            //System.Diagnostics.Process.Start("explorer.exe", path);
            ////System.Diagnostics.Process.Start(path); //打开此文件。
            // Retrieve all running processes on the local computer.
            Process[] processlist = Process.GetProcesses();

            // Iterating through the running processses, and Killing the process that is holding your previosly opened Excel file.
            foreach (Process runningProcess in processlist)
            {
                if (runningProcess.MainWindowTitle.Contains(path))
                    runningProcess.Kill();
            }

            ExportToggle(false);
            try
            {
                ultraGridExcelExporter1.ExportAsync(ultraGrid1, path);
                //System.Diagnostics.Process.Start("explorer.exe", path);
                ExportToggle(true);
                //Msg.DisplayMsg("", "导出文件'" + path + "'", 1200);
                Msg.DisplayMsg("提示 ", "导出文件在：'" + path + "'", 6000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                ExportToggle(true);
            }
        }




        private void btnSumTG_Click(object sender, EventArgs e)
        {
            try
            {
                
                others.myGZ gz = new myGZ();
                DataSet ds = gz.GetDateGZTG(Convert.ToDateTime(this.dtpDateStart.Text), Convert.ToDateTime(this.dtpDateEnd.Text));
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btnExcept_Click(object sender, EventArgs e)
        {
            try
            {

                others.myGZ gz = new myGZ();
                DataSet ds = gz.GetDateGZEXCEPT(Convert.ToDateTime(this.dtpDateStart.Text), Convert.ToDateTime(this.dtpDateEnd.Text));
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn运算_Click(object sender, EventArgs e)
        {
            others.xlmoon xl = new xlmoon();
            //int flag = xl.opration(this.dtpDateStart.Text, dtpDateEnd.Text, cmbBM.Text.Trim());

            //MessageBox.Show(flag.ToString());
            if (xl.opration(this.dtpDateStart.Text, dtpDateEnd.Text, cmbBM.Text.Trim()) > 0)
            {
                MessageBox.Show("运算成功!");
            }
            else { MessageBox.Show("运算出错!，请稍后再试"); }
        }

        private void btn本部技能工资汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.BBJINZHZ( this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn单位岗位工资汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.unitGWGZHZ(this.cmbBM.Text.Trim(), this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn本部岗位工资汇总_Click(object sender, EventArgs e)
        {
            
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.BBGWGZHZ( this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        
        }

        private void btn单位养老金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.unitYLJHV(this.cmbBM.Text.Trim(), this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn本部养老金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.BBYLJHZ(this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }

        }

        private void btn单位失业金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.unitSYJHZ(this.cmbBM.Text.Trim(), this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn本部失业金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.BBSYJHZ(this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn单位医疗金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.unitYLJHZ(this.cmbBM.Text.Trim(), this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn本部医疗金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.BBYiLiaoJHZ(this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn单位公积金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.unitGJJHZ(this.cmbBM.Text.Trim(), this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn本部公积金汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.BBGJJHZ(this.dtpDateEnd.Text);
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn减人清单_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.ReducePeopleList(this.cmbBM.Text .Trim ());
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn减人汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.ReducePeopleSum(this.cmbBM.Text.Trim());
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }

        }

        private void btn增人清单_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.AddPeopleList(this.cmbBM.Text.Trim());
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn增人汇总_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.AddPeopleSum(this.cmbBM.Text.Trim());
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn不变人员项目变化清单_Click(object sender, EventArgs e)
        {
            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.NotChange();
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn不变人员二月工资汇总_Click(object sender, EventArgs e)
        {

            try
            {
                others.xlmoon xl = new xlmoon();

                DataSet ds = xl.NotChangeSum();
                this.BindData(ds);

            }
            catch (Exception ex) { LOG.Error(ex); }
        }

        private void btn三种用工统计_Click(object sender, EventArgs e)
        {
            try
            {

                others.xlmoon xl = new xlmoon();
                if (xl.Creat3YGTJ(dtpDateEnd.Text) > 0)
                {
                    DataSet ds = xl.Select3YGTJ();
                    this.BindData(ds);
                    xl.Drop3YGTJ();
                }
                

            }
            catch (Exception ex) { LOG.Error(ex); }
        }
        
    }
}
